package icst.cyber.hrm.bo;

import java.io.ByteArrayInputStream;
import java.security.MessageDigest;

/**
 * @author NGOCLAN
 * 
 */
public class CommonBO {

	private static String toHexadecimal(byte[] data) {
		String result = "";
		ByteArrayInputStream input = new ByteArrayInputStream(data);
		String cadAux;
		boolean ult0 = false;
		int leido = input.read();
		while (leido != -1) {
			cadAux = Integer.toHexString(leido);
			if (cadAux.length() < 2) {
				result += "0";
				if (cadAux.length() == 0)
					ult0 = true;
			} else {
				ult0 = false;
			}
			result += cadAux;
			leido = input.read();
		}
		if (ult0)
			result = result.substring(0, result.length() - 2)
					+ result.charAt(result.length() - 1);
		return result;
	}

	public static String encryptMD5(String code) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] input = code.getBytes(); // "UTF8");
			input = md.digest(input);
			code = toHexadecimal(input); // new String(input,"UTF8");

			return code;
		} catch (Exception e) {
			return code;
		}
	}
}
